//index.js
import { getHomeCateData } from "../common/netTool.js"
const app = getApp()
Page({
  data: {
    cateColor: "#949494",
    lastIndex: 0,
    currentHeight: 0,
    dataList: [],
    bannerData: null,
    columnData: null,
    recommendData: null,
    isStandard: false
  },
  onLoad: function() {
    this.getCateData()
  },
  cateClick: function (e) {
    this.cateInnerCtrl(e.currentTarget.dataset.index)
  },
  cateInnerCtrl: function(index) {
    var that = this
    var data = that.data.columnData
    var currentIndex = index
    var item = data[that.data.lastIndex]
    item.selected = false
    var currentItem = data[currentIndex]
    currentItem.selected = true
    that.setData({
      columnData: data,
      lastIndex: currentIndex
    })
    this.getHomeData();
  },
  getCateData: function () {
    var that = this
    app.fetchPostApi('news/main/getBannerAndColumn').then(data => {
      for (var i = 0; i < data.appBannerData.length; i++) {
        var item = data.appBannerData[i]
        item.imgUrl = app.globalData.base_image_url + item.imgUrl
      }
      var newDataList = that.data.dataList
      for (var i = 0; i < data.appColumnData.length; i++) {
        var item = data.appColumnData[i]
        item.pageNow = '1'
        newDataList.push([]);
      }
      for (var i = 0; i < data.featuredBanner.length; i++) {
        var item = data.featuredBanner[i]
        item.imgUrl = app.globalData.base_image_url + item.imgUrl
      }
      data.appColumnData[0].selected = true;
      that.setData({
        dataList: newDataList,
        bannerData: data.appBannerData,
        columnData: data.appColumnData,
        recommendData: data.featuredBanner,
      })
      that.cateInnerCtrl(0)

      // console.log('data is',data)
    })
  },
  getHomeData: function(isLoadMore) {
    var that = this
    var data = that.data.columnData
    var item = data[that.data.lastIndex]
    app.fetchPostApi('news/getNews',{
      "size": "10",
      "page": item.pageNow,
      "columnId": item.id,
      "isLoadmore": true
    }).then(data => {
      var that = this
      var newList = that.data.dataList
      var currentItem = newList[that.data.lastIndex]
      for (var i = 0; i < data.content.length; i++) {
        var item = data.content[i]
        item.logo[0] = app.globalData.base_image_url + item.logo[0]
      }
      if (isLoadMore || currentItem.length == 0) {
        for (var i = 0; i < data.content.length; i++) {
          var item = data.content[i]
          item.createTime = app.changeTime(item.createTime)
          currentItem.push(item)
        }
      }
      that.setData({
        dataList: newList,
        currentHeight: newList[that.data.lastIndex].length*600
      })
      // console.log('data is',that.data.dataList)
    })
  },
  changePage: function(event) {
    this.cateInnerCtrl(event.detail.current)
  },
  loadMore: function () {
    var that = this
    var data = that.data.columnData
    var item = data[that.data.lastIndex]
    item.pageNow = parseInt(item.pageNow) + 1
    that.setData({
      columnData: data
    })
    that.getHomeData(true);
  },
  detailPage: function(e) {
    console.log(e.currentTarget.dataset.id)
    var nextUrl = './detail/indexDetail?id=' + e.currentTarget.dataset.id
    wx.navigateTo({
      url: nextUrl,
    })
  },
  onPageScroll: function (e) {
    console.log(wx.getSystemInfoSync().windowWidth)
    var height = 750 / wx.getSystemInfoSync().windowWidth * e.scrollTop
    this.setData({
      isStandard: height > 1000
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getCateData()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
